package com.eric.windows.anima;

import android.animation.Animator;
import android.animation.FloatEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.util.FloatProperty;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import com.eric.windows.R;

public class AnimatorTestActivity extends Activity  {

    private ImageView img;

    @TargetApi(Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animator_test);
        img = (ImageView) findViewById(R.id.img);
        ValueAnimator animator = ValueAnimator.ofFloat(0, 500);
        // 这里指定变化持续时间
        animator.setDuration(1000);
        //设置插值器
        animator.setInterpolator(new LinearInterpolator());
        //设置估值器
        animator.setEvaluator(new FloatEvaluator());
        //设置动画重复的模式
        animator.setRepeatMode(ValueAnimator.RESTART);
        //设置动画重复的次数 -1 无限次
        animator.setRepeatCount(ValueAnimator.INFINITE);
        //设置帧刷新延迟
        ValueAnimator.setFrameDelay(5000);

        //开始动画
        animator.start();
        //开始动画后，我们可以动态的获取变化值
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
            @Override
            public void onAnimationUpdate(ValueAnimator animation){
                //根据变化值来设置imageView对象的Y轴坐标，这样就实现了imageView的垂直移动
                img.setTranslationY((Float) animation.getAnimatedValue());
            }
        });


        LinearInterpolator timeInterpolator = (LinearInterpolator) animator.getInterpolator();


        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(img, "translationX", 0.0f, 350.0f, 0f);
        objectAnimator.setDuration(2500).start();
        objectAnimator.setInterpolator(new BounceInterpolator());//动画插值
        objectAnimator.setRepeatCount(-1);//设置动画重复次数
        objectAnimator.setRepeatMode(ValueAnimator.RESTART);//动画重复模式
        objectAnimator.setStartDelay(1000);//动画延时执行
        objectAnimator.start();//启动动画
        objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
            @Override
            public void onAnimationUpdate(ValueAnimator animation){
                //根据变化值来设置imageView对象的Y轴坐标，这样就实现了imageView的垂直移动
//                img.setTranslationY((Float) animation.getAnimatedValue());
            }
        });

        objectAnimator.addListener(new Animator.AnimatorListener() {
            //动画开始
            @Override
            public void onAnimationStart(Animator animation) {
            }
            //动画结束
            @Override
            public void onAnimationEnd(Animator animation) {

            }
            //动画取消
            @Override
            public void onAnimationCancel(Animator animation) {

            }
            //动画重复
            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        objectAnimator.addPauseListener(new Animator.AnimatorPauseListener() {
            //动画暂停
            @Override
            public void onAnimationPause(Animator animation) {

            }
            //动画重启
            @Override
            public void onAnimationResume(Animator animation) {

            }
        });



        CircleAnim  mCircleAnim = (CircleAnim) findViewById(R.id.circle);
        final ObjectAnimator animator2 = ObjectAnimator.ofFloat(mCircleAnim, new FloatProperty<CircleAnim>("pivotX") {
            @Override
            public void setValue(CircleAnim object, float value) {
                object.setRadius(value);
            }

            @Override
            public Float get(CircleAnim object) {
                return object.getRadius();
            }
        }, 0, 100);
        animator.setDuration(5000);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animator2.start();
            }
        });


    }

    private float dp2px(float dp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }


}
